/***
 Wrapper/Helper Class for datagrid based on jQuery Datatable Plugin
 ***/

var Datatable = function () {

    var tableOptions;  // main options
    var dataTable; // datatable object
    var table;    // actual table jquery object
    var tableContainer;    // actual table container object
    var tableWrapper; // actual table wrapper jquery object
    var tableInitialized = false;
    var ajaxParams = []; // set filter mode
    var actionParams = [];


    var countSelectedRecords = function () {
        var selected = $('tbody > tr > td:nth-child(1) input[type="checkbox"]:checked', table).size();
        var text = tableOptions.dataTable.oLanguage.sGroupActions;
        if (selected > 0) {
            $('.table-group-actions > span', tableWrapper).text(text.replace("_TOTAL_", selected));
        } else {
            $('.table-group-actions > span', tableWrapper).text("");
        }
    };

    return {

        //main function to initiate the module
        init: function (options) {

            if (!$().dataTable) {
                return;
            }

            var the = this;

            // default settings
            options = $.extend(true, {
                src                           : "",  // actual table
                filterApplyAction             : "filter",
                filterCancelAction            : "filter_cancel",
                resetGroupActionInputOnSuccess: true,
                dataTable                     : {
                    'sDom': "<'table-scrollable't><'row'<'col-md-8 col-sm-12'pli><'col-md-4 col-sm-12'>r>>", // datatable layout

                    'aLengthMenu'   : [ // set available records per page
                        [10, 25, 50, 100, 150],
                        [10, 25, 50, 100, 150]
                    ],
                    'iDisplayLength': 10, // default records per page
                    'oLanguage'     : {  // language settings
                        'sProcessing'             : '<img src="' + g.ctx + 'static/admin/img/loading-spinner-grey.gif"/><span>&nbsp;&nbsp;加载数据...</span>',
                        'sInfoFiltered'           : '(数据表中共为 _MAX_ 条记录)',
                        'sLengthMenu'             : '&nbsp;&nbsp;<span class="seperator">|</span>&nbsp;&nbsp;每页 _MENU_条',
                        'sZeroRecords'            : '对不起，查询不到任何相关数据',
                        'sInfo'                   : "&nbsp;&nbsp;<span class='seperator'>|</span>&nbsp;&nbsp; 显示从 _START_ 到 _END_，总 _TOTAL_ 条",
                        'sInfoEmpty'              : '没有任何数据可以显示',
                        'sAjaxRequestGeneralError': '获取服务器数据出现问题',
                        'oPaginate'               : {
                            'sFirst'   : "第一页",
                            'sPrevious': "上一页 ",
                            'sNext'    : "下一页 ",
                            'sLast'    : "最后一页 ",
                            'sPage'    : "第",
                            'sPageOf'  : "页"
                        },
                        'sGroupActions'           : "已选择 _TOTAL_ 条:  ",
                        'sEmptyTable'             : "您可以在通过搜索进行检索数据"

                    },

                    "aoColumnDefs": [
                        {  // define columns sorting options(by default all columns are sortable extept the first checkbox column)
                            'bSortable': false,
                            'aTargets' : [ 0 ]
                        }
                    ],

                    "bAutoWidth"     : false,   // disable fixed width and enable fluid table
                    "bSortCellsTop"  : true, // make sortable only the first row in thead
                    "sPaginationType": "bootstrap_extended", // pagination type(bootstrap, bootstrap_full_number or bootstrap_extended)
                    "bProcessing"    : true, // enable/disable display message box on record load
                    "bServerSide"    : true, // enable/disable server side ajax loading
                    "sAjaxSource"    : "", // define ajax source URL
                    "sServerMethod"  : "POST",

                    // handle ajax request
                    "fnServerData"   : function (sSource, aoData, fnCallback, oSettings) {
                        oSettings.jqXHR = $.ajax({
                            "dataType": 'json',
                            "type"    : "POST",
                            "url"     : sSource,
                            "data"    : aoData,
                            "success" : function (res, textStatus, jqXHR) {
                                if (res.sMessage) {
                                    App.alert({type: (res.sStatus == 'OK' ? 'success' : 'danger'), icon: (res.sStatus == 'OK' ? 'check' : 'warning'), message: res.sMessage, container: tableWrapper, place: 'prepend'});
                                }
                                if (res.sStatus) {
                                    if (tableOptions.resetGroupActionInputOnSuccess) {
                                        $('.table-group-action-input', tableWrapper).val("");
                                    }
                                }
                                if ($('.group-checkable', table).size() === 1) {
                                    $('.group-checkable', table).attr("checked", false);
                                    $.uniform.update($('.group-checkable', table));
                                }
                                if (tableOptions.onSuccess) {
                                    tableOptions.onSuccess.call(the);
                                }
                                fnCallback(res, textStatus, jqXHR);
                            },
                            "error"   : function () {
                                if (tableOptions.onError) {
                                    tableOptions.onError.call(the);
                                }
                                App.alert({type: 'danger', icon: 'warning', message: tableOptions.dataTable.oLanguage.sAjaxRequestGeneralError, container: tableWrapper, place: 'prepend'});
                                $('.dataTables_processing', tableWrapper).remove();
                            }
                        });
                    },

                    // pass additional parameter
                    "fnServerParams" : function (aoData) {
                        //here can be added an external ajax request parameters.
                        for (var i in ajaxParams) {
                            var param = ajaxParams[i];
                            aoData.push({"name": param.name, "value": param.value});
                        }
                        if (actionParams.length > 0) {
                            for (var ai in actionParams) {
                                var aparam = actionParams[ai];
                                var na = aparam.name;
                                aoData.push({"name": na, "value": aparam.value});
                            }
                        }
                    },

                    "fnDrawCallback": function (oSettings) { // run some code on table redraw
                        if (tableInitialized === false) { // check if table has been initialized
                            tableInitialized = true; // set table initialized
                            table.show(); // display table
                        }
                        App.initUniform($('input[type="checkbox"]', table));
                        countSelectedRecords(); // reset selected records indicator
                    }
                }
            }, options);

            tableOptions = options;

            // create table's jquery object
            table = $(options.src);
            tableContainer = table.parents(".table-container");

            // apply the special class that used to restyle the default datatable
            $.fn.dataTableExt.oStdClasses.sWrapper = $.fn.dataTableExt.oStdClasses.sWrapper + " dataTables_extended_wrapper";

            // initialize a datatable
            dataTable = table.dataTable(options.dataTable);

            tableWrapper = table.parents('.dataTables_wrapper');

            // modify table per page dropdown input by appliying some classes
            $('.dataTables_length select', tableWrapper).addClass("form-control input-xsmall input-sm");
            $('.dataTables_length select', tableWrapper).select2({
                minimumResultsForSearch: "-1"
            });

            // build table group actions panel
            if ($('.table-actions-wrapper', tableContainer).size() === 1) {
                $('.table-group-actions', tableWrapper).html($('.table-actions-wrapper', tableContainer).html()); // place the panel inside the wrapper
                $('.table-actions-wrapper', tableContainer).remove(); // remove the template container
            }
            // handle group checkboxes check/uncheck
            $('.group-checkable', table).change(function () {
                var set = $('tbody > tr > td:nth-child(1) input[type="checkbox"]', table);
                var checked = $(this).is(":checked");
                $(set).each(function () {
                    $(this).attr("checked", checked);
                });
                $.uniform.update(set);
                countSelectedRecords();
            });

            // handle row's checkbox click
            table.on('change', 'tbody > tr > td:nth-child(1) input[type="checkbox"]', function () {
                countSelectedRecords();
            });

            table.on('keydown', 'tr.filter .form-control', function (e) {
                if (e.keyCode == "13") {//keyCode=13是回车键
                    $('button.filter-submit').click();
                }
            });

            // handle filter submit button click
            table.on('click', '.filter-submit', function (e) {
                e.preventDefault();

                var id = App.getUniqueID("app_alert");
                if (id) {
                    $('#' + id).remove();
                }

                the.addAjaxParam("sAction", tableOptions.filterApplyAction);

                // get all typeable inputs
                $('textarea.form-filter, select.form-filter, input.form-filter:not([type="radio"],[type="checkbox"])', table).each(function () {
                    the.addAjaxParam($(this).attr("name"), $(this).val());
                });

                // get all checkable inputs
                $('input.form-filter[type="checkbox"]:checked, input.form-filter[type="radio"]:checked', table).each(function () {
                    the.addAjaxParam($(this).attr("name"), $(this).val());
                });

                dataTable.fnDraw();
                the.clearAjaxParams();
            });

            // handle filter cancel button click
            table.on('click', '.filter-cancel', function (e) {
                e.preventDefault();
                $('textarea.form-filter, select.form-filter, input.form-filter', table).each(function () {
                    $(this).val("");
                });
                $('input.form-filter[type="checkbox"]', table).each(function () {
                    $(this).attr("checked", false);
                });
                the.addAjaxParam("sAction", tableOptions.filterCancelAction);
                dataTable.fnDraw();
                the.clearAjaxParams();
            });
        },

        getSelectedRowsCount: function () {
            return $('tbody > tr > td:nth-child(1) input[type="checkbox"]:checked', table).size();
        },

        getSelectedRows: function () {
            var rows = [];
            $('tbody > tr > td:nth-child(1) input[type="checkbox"]:checked', table).each(function () {
                rows.push({name: $(this).attr("name"), value: $(this).val()});
            });

            return rows;
        },

        addActionAjaxParam   : function (name, value) {
            actionParams.push({"name": name, "value": value});
        },
        removeActionAjaxParm : function (name) {
            for (var param in actionParams) {
                if (actionParams[param].name == name) {
                    actionParams.splice(param, 1);
                    return;
                }
            }
        },
        clearActionAjaxParams: function () {
            actionParams = [];
        },
        addAjaxParam         : function (name, value) {
            ajaxParams.push({"name": name, "value": value});
        },
        clearAjaxParams      : function () {
            ajaxParams = [];
        },

        getDataTable: function () {
            return dataTable;
        },

        getTableWrapper: function () {
            return tableWrapper;
        },

        gettableContainer: function () {
            return tableContainer;
        },

        getTable: function () {
            return table;
        }

    };

};